﻿
namespace DWLib
{
    /// <summary>
    /// The digit class forces restrains on its value based on the given minimum and maximum.
    /// </summary>
    internal abstract class Digit : IOperable
    {
        /// <summary>
        /// get the value of digit
        /// </summary>
        public int Value
        {
            get
            {
                return number;
            }
            internal set
            {
                //ensures the value is within bounds
                number = (value < min) ? min : ((value > max) ? max : value);
            }
        }

        private int number;
        private int min;
        private int max;

        public Digit(int min, int max)
        {
            //set the minimum and maximum value
            this.min = min;
            this.max = max;
            number = min;
        }
        /// <summary>
        /// Increments the digit 
        /// </summary>
        /// <returns>true if digit higher than maximum then the value will be reset otherwise false</returns>
        public bool increment()
        {
            if (++number > max)
            {
                number = min;
                return true;
            }

            return false;
        }
        /// <summary>
        /// Decrements the digit
        /// </summary>
        /// <returns>true if digit lower than minimum then the value will be reset otherwise false</returns>
        public bool decrement()
        {
            if (--number < min)
            {
                number = max;
                return true;
            }

            return false;
        }
    }
}
